{extend name="public:index"/}
{block name="title"}{:sysconf('title')}{/block}
{block name="content"}
<section class="My_shop">
    <!--<form id="cart_form" action="{:url('Order/order')}" method="get">-->
        <div class="top">
            <p class="p1"></p>
            <p class="p2">购物车</p>
            <p class="p3"><i>编辑</i><em>完成</em></p>
        </div>
    {if condition="$list"}
    <div class="shopmain">
            <ul>
                {foreach name="list" item="vo"}
                <li>
                    <label><input name="cartId" type="checkbox" value="{$vo['id']}" /></label>
                    <div class="center">
                        <a href="{:url('Goods/show',['id'=>$vo['goods_id']])}"><img src="{$vo['goods_img_url']}" alt=""></a>
                    </div>
                    <div class="right">
                        <b><a href="{:url('Goods/show',['id'=>$vo['goods_id']])}">{$vo['goods_title']}</a></b>
                        <p class="p1">小计：￥<em>{$vo['goods_sell_price']*$vo['goods_count']}</em></p>
                        <p class="p2"><em>￥<i class="szi">{$vo['goods_sell_price']}</i></em>
                            <a href="javascript:;" class="J_add" data-id="{$vo['id']}">+</a>
                            <input type="text" value="{$vo['goods_count']}" class="J_input" readonly />
                            <a href="javascript:;" class="J_minus" data-id="{$vo['id']}">-</a>
                        </p>
                    </div>
                    <div class="delete" data-id="{$vo['id']}">删除</div>
                </li>
                {/foreach}
            </ul>
            <p style="height:3rem;"></p>
        </div>
        <div class="jiesuan">
            <div class="left">
                <label class="quan quan1" onclick="selectAll()"><em>全选</em></label>
                <label class="quan quan2" style="display:none;" onclick="selectAll1()"><em>全选</em></label>
                <!-- <button onclick="selectAll()"/>全选</button>  -->
                <p>合计：￥<em id="zh">0</em></p>
            </div>
            <div class="right"><a href="javascript:;" id="paymoney">去结算<em></em></a></div>
        </div>
    {else/}
    <div class="nofind"><img src="__INDEX__/images/cart2.png"><p>购物车暂时为空</p></div>
    {/if}
    <!--</form>-->
</section>
{include file="include/nav"}
<script type="text/javascript">
    /*删除购物车*/
    $('.My_shop .shopmain ul li .delete').click(function(){
        var obj = this;
        var cartId = $(obj).data('id');
        $.ajax({
            type: "POST",
            url: "{:url('Cart/del')}",
            data: {'cartId':cartId},
            dataType:"JSON",
            success:function(res){
                if (res.code == 0) {
                    layer.msg(res.msg,{icon:6});
                    $(obj).parent().remove();
                } else {
                    layer.msg(res.msg,{icon:5});
                }
            },
            error: function(){
                layer.msg("网络延迟，发送失败",{icon:5});
            }
        });
    });
    /*加*/
    $(".shopmain ul li .right .J_add").click(function(){
        var obj = this;
        var cartId = $(obj).data('id');
        $.ajax({
            type: "POST",
            url: "{:url('Cart/goodsCount')}",
            data: {'cartId':cartId,'action':'jia'},
            dataType:"JSON",
            success:function(res){
                if (res.code == 0) {
                    // layer.msg(res.msg,{icon:6});
                    $(obj).siblings('.J_input').val(res.data.goods_count);
                    $(obj).parent('.p2').siblings('.p1').children('em').html((res.data.goods_price_sum));
                    var dj = $(obj).siblings('em').children('.szi').html();
                        yy = $('#zh').html();
                        zz = parseInt(yy)+parseInt(dj)
                    $('#zh').html(zz);
                } else {
                    layer.msg(res.msg,{icon:5});
                }
            },
            error: function(){
                layer.msg("网络延迟，发送失败",{icon:5});
            }
        });
    });
    /*减*/
    $(".shopmain ul li .right .J_minus").click(function(){
        var obj = this;
        var cartId = $(obj).data('id');
        var count = $(obj).siblings('.J_input').val();
        if (parseInt(count) == 1) {
            return false;
        }
        $.ajax({
            type: "POST",
            url: "{:url('Cart/goodsCount')}",
            data: {'cartId':cartId,'action':'jian'},
            dataType:"JSON",
            success:function(res){
                if (res.code == 0) {
                    // layer.msg(res.msg,{icon:6});
                    $(obj).siblings('.J_input').val(res.data.goods_count);
                    $(obj).parent('.p2').siblings('.p1').children('em').html((res.data.goods_price_sum));
                    var dj = $(obj).siblings('em').children('.szi').html();
                        yy = $('#zh').html();
                        zz = parseInt(yy)-parseInt(dj)
                    $('#zh').html(zz);
                } else {
                    layer.msg(res.msg,{icon:5});
                }
            },
            error: function(){
                layer.msg("网络延迟，发送失败",{icon:5});
            }
        });
    });
    $('.My_shop .shopmain ul li label').click(function(){
        $('.quan1').show();
        $('.quan2').hide();
         if ($(this).hasClass('lab')) {
                $(this).removeClass('lab');
                $(this).removeClass('canlab');
                $('.jiesuan .left input').removeClass('lab1');
                $('.jiesuan .left .quan').removeClass('canlab1');
                var dq = $(this).siblings('.right').children('.p1').children('em').html();
                    yy = $('#zh').html();
                    zz = parseInt(yy)-parseInt(dq)
                $('#zh').html(zz);
         } else {
                $(this).addClass('lab');
                $(this).addClass('canlab');
                var dq = $(this).siblings('.right').children('.p1').children('em').html();
                    yy = $('#zh').html();
                    zz = parseInt(dq)+parseInt(yy)
                $('#zh').html(zz);
         }
    });
    var zonj = 0;
    for (var i = 0; i < $('.My_shop .shopmain ul li').length; i++) {  
        zonj +=  parseInt($('.My_shop .shopmain ul li').eq(i).children('.right').children('.p1').children('em').html()) 
    } 
    function selectAll(){  
        $('.quan1').hide();
        $('.quan2').show();
        var checkBoxArr = document.getElementsByName("cartId");  
        for (var i = 0; i < checkBoxArr.length; i++) {  
            checkBoxArr[i].checked = true;  
        }  
        $('.jiesuan .left .quan').addClass('canlab1');
        $('.My_shop .shopmain ul li label').addClass('lab');
        $('.My_shop .shopmain ul li label').addClass('canlab');
        // var items1 = $('.My_shop .shopmain ul li label').length; 
        // $(".jiesuan .right em").html('('+items1+')');
        $('.bianji .left input').addClass('lab2');
        $('.bianji .left .quan').addClass('canlab1');
        $('#zh').html(zonj);
    } 
    function selectAll1(){  
        $('.quan1').show();
        $('.quan2').hide();
        var checkBoxArr = document.getElementsByName("cartId");  
        for (var i = 0; i < checkBoxArr.length; i++) {  
            checkBoxArr[i].checked = false;  
        }  
        $('.jiesuan .left .quan').removeClass('canlab1');
        $('.My_shop .shopmain ul li label').removeClass('lab');
        $('.My_shop .shopmain ul li label').removeClass('canlab');
        // var items1 = $('.My_shop .shopmain ul li label').length; 
        // $(".jiesuan .right em").html('('+items1+')');
        $('.bianji .left input').removeClass('lab2');
        $('.bianji .left .quan').removeClass('canlab1');
        $('#zh').html(0);
    } 
    $("#paymoney").click(function () {
        var length1 = $("input:checkbox[name='cartId']:checked").length;
        if (length1 <= 0)
        {
            layer.msg('请添加或选择您要购买的商品');return
        }
        var cartIds = '';
        $("input:checkbox[name='cartId']:checked").each(function() {
            cartIds += $(this).val() + ",";
        });
        window.location.href="{:url('Order/order')}?cartIds="+cartIds;
    });
</script>
{/block}